package com.scott.customer.service;

import com.netflix.hystrix.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author guanjiwei
 */

public class MyHystrixDetail extends HystrixCommand<String> {

    private  SayHelloService sayHelloService;

    public MyHystrixDetail(SayHelloService sayHelloService) {
        super(Setter.withGroupKey(

                HystrixCommandGroupKey.Factory.asKey("sayHelloService"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("hello"))

                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        //至少有10个请求，熔断器才进行错误率的计算
                        .withCircuitBreakerRequestVolumeThreshold(10)
                        //熔断器中断请求5秒后会进入半打开状态,放部分流量过去重试
                        .withCircuitBreakerSleepWindowInMilliseconds(5000)
                        //错误率达到50开启熔断保护
                        .withCircuitBreakerErrorThresholdPercentage(50)
                        .withExecutionTimeoutEnabled(true))

                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties
                        .Setter().withCoreSize(10)));
        this.sayHelloService = sayHelloService;
    }

    @Override
    protected String run() throws Exception {
        System.out.println(Thread.currentThread().getName());
        return sayHelloService.hello();
    }

    @Override
    protected String getFallback() {
        return "error detail";
    }
}
